package com.google.code.avajhelpdesk.model;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.avajhelpdesk.entities.DomainModel;
import com.google.code.avajhelpdesk.entities.Entities;
import com.google.code.avajhelpdesk.persistence.IGenericDAO;

public class GenericLoadableDetachableModel<T extends DomainModel> extends LoadableDetachableModel<T> {

	/**
	 * Serial version 
	 */
	private static final long serialVersionUID = -775741614072858469L;
	
	private static final Logger logger = LoggerFactory.getLogger(GenericLoadableDetachableModel.class);
	
	@SpringBean 
    private IGenericDAO genericDAO;
    
	private Class<T> entityClass;

	private Long entityId;
    
	public GenericLoadableDetachableModel(T entity) {
        super(entity);
        this.entityId = entity.getId();
        this.entityClass = Entities.getEntityClass(entity);
        InjectorHolder.getInjector().inject(this);
    }

    public GenericLoadableDetachableModel(Class<T> entityClass, 
            Long entityId) {
        super();
        this.entityClass = entityClass;
        this.entityId = entityId;
        InjectorHolder.getInjector().inject(this);
    }

    protected T load() {
    	logger.debug("Load entiy id=" + entityId);
    	return genericDAO.get(entityClass, entityId);
    }	
    
}